<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel=StyleSheet HREF="../stylesheet/stylesheet.css" TYPE="text/css" MEDIA=screen>
<title>Shop Management System</title>
<?php include('menu.php'); 
include("../connect/connect.php");
?>
</head>
<body>
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
<div class="heading">
  <h1>รายงานสำหรับผู้บริหาร</h1>
</div>
    <div class="content">
      <div style="display: inline-block; width: 100%; margin-bottom: 15px; clear: both;">
      <div style="float: left; width: 49%;">
        <div style="background: #547C96; color: #FFF; border-bottom: 1px solid #8EAEC3; padding: 5px 5px 5px 10px; font-size: 14px; font-weight: bold;">ภาพโดยรวม</div>
        <div style="background: #FCFCFC; border: 1px solid #8EAEC3; padding: 10px; height: 180px;">
          <table cellpadding="5" style="width: 100%;">
            <tr>
              <td width="60%">ผลกำไร-ขาดทุนทั้งหมด</td>
              <td align="right">
<?php 
	$Query = "select t1.year1,t1.amount,t1.cost,t2.expend from (select SUM(amount) as amount, sum(cost) as cost, year(date) as year1 from sale group by year (date)) as t1 left join (select SUM(amount) as expend, year(date) as year2 from expend group by year(date)) as t2 on t1.year1=t2.year2 ORDER BY t1.year1,t2.year2";
	$objQuery = mysql_query($Query) or die ("Error Query [".$Query."]");
	$strTotal=0;
	while($row = mysql_fetch_array($objQuery))
		{
			$amount = $row["amount"];
		  	$cost = $row["cost"];
		  	$expend = $row["expend"];
		  	$profit = ($amount-$cost)-$expend;
			$strTotal=$strTotal+$profit;
		}
			echo number_format($strTotal, 2,'.',',')." บาท";

?></td>
            <tr>
              <td>ผลกำไร-ขาดทุนปีปัจจุบัน</td>
              <td align="right">
<?php 
	$year = date("Y");
	$Query1 = "select t1.year1,t1.amount,t1.cost,t2.expend from (select SUM(amount) as amount, sum(cost) as cost, year(date) as year1 from sale WHERE year(date) = '$year' group by year (date)) as t1 left join (select SUM(amount) as expend, year(date) as year2 from expend group by year(date)) as t2 on t1.year1=t2.year2 ORDER BY t1.year1,t2.year2";
	$objQuery1 = mysql_query($Query1) or die ("Error Query [".$Query1."]");
	$strTotal1=0;
	while($row1 = mysql_fetch_array($objQuery1))
		{
			$amount1 = $row1["amount"];
		  	$cost1 = $row1["cost"];
		  	$expend1 = $row1["expend"];
		  	$profit1 = ($amount1-$cost1)-$expend1;
			$strTotal1=$strTotal1+$profit1;
		}
			echo number_format($strTotal1, 2,'.',',')." บาท";

?></td>
            </tr>
            <tr>
              <td>ยอดขายทั้งหมด</td>
              <td align="right">
<?php 
	$Query1 = "SELECT SUM(amount) as amount FROM sale";
	$objQuery1 = mysql_query($Query1) or die ("Error Query [".$Query1."]");
	$row1 = mysql_fetch_array($objQuery1);
			echo number_format($row1["amount"], 2,'.',',')." บาท";

?></td>
            </tr>
            <tr>
              <td>ยอดขายปีปัจจุบัน</td>
              <td align="right"><?php 
	$year = date("Y");
	$Query1 = "SELECT SUM(amount) as amount FROM sale WHERE year(date) = $year";
	$objQuery1 = mysql_query($Query1) or die ("Error Query [".$Query1."]");
	$row1 = mysql_fetch_array($objQuery1);
			echo number_format($row1["amount"], 2,'.',',')." บาท";

?></td>
            </tr>
            <tr>
              <td>ยอดขายเดือนปัจจุบัน</td>
              <td align="right"><?php 
	$month = date("m");
	$Query1 = "SELECT SUM(amount) as amount FROM sale WHERE month(date) = $month";
	$objQuery1 = mysql_query($Query1) or die ("Error Query [".$Query1."]");
	$row1 = mysql_fetch_array($objQuery1);
			echo number_format($row1["amount"], 2,'.',',')." บาท";

?></td>
            </tr>
            <tr>
              <td>ยอดลูกหนี้ค้างชำระทั้งหมด</td>
              <td align="right"><?php 
	$Query1 = "SELECT sum(deb_amount) as deb_amount FROM debtor WHERE deb_status = '0'";
	$objQuery1 = mysql_query($Query1) or die ("Error Query [".$Query1."]");
	$row1 = mysql_fetch_array($objQuery1);
			echo number_format($row1["deb_amount"], 2,'.',',')." บาท";

?></td>
            </tr>
          </table>
        </div>
      </div>
      <div style="float: right; width: 49%;">
	  <div style="background: #547C96; color: #FFF; border-bottom: 1px solid #8EAEC3; padding: 5px 5px 5px 10px; font-size: 14px; font-weight: bold;">สถิติยอดขายสินค้าประจำเดือนนี้</div>
        <div align="center" style="background: #FCFCFC; border: 1px solid #8EAEC3; padding: 10px; height: 49%;">
          <div id="report" align="center" style="max-width:550px;"><img src="../jpgraph/static.php" width="100%" height="180px" /></div>
        </div>
      </div>
    </div>
    <div>
      <div style="background: #547C96; color: #FFF; border-bottom: 1px solid #8EAEC3; padding: 5px 5px 5px 10px; font-size: 14px; font-weight: bold;">สรุปสินค้าขายดี - ไม่ดี 10 อันดับ</div>
      <div style="background: #FCFCFC; border: 1px solid #8EAEC3; padding: 10px;">
        <table width="90%" border="0" align="center" cellpadding="20" cellspacing="0">
        <tr>
          <td><table class="list">
            <thead>
              <tr>
                <td colspan="2" class="center"><font size="3">สินค้าขายดี</font></td>
                </tr>
			 </thead>
            <tbody>
              <tr class="filter">
                <td width="30%" class="center"><strong>ลำดับ</strong></td>
                <td width="70%" class="center"><strong>ชื่อสินค้า</strong></td>
              </tr>
<?php
	$num1=0;
	$sql1 = "SELECT name FROM product ORDER BY selling DESC LIMIT 10";
	$query1 = mysql_query($sql1) or die ("Error Query [".$sql1."]");
	while($row1=mysql_fetch_array($query1)){
	$num1++;
?>
              <tr>
                <td class="center"><?php echo $num1; ?></td>
                <td class="left" style="padding-left:30px"><?php echo $row1["name"]; ?></td>
              </tr>
<?php
}
?>
            </tbody>
          </table></td>
          <td>&nbsp;</td>
          <td><table class="list">
            <thead>
              <tr>
                <td colspan="2" class="center"><font size="3">สินค้าขายไม่ดี</font></td>
                </tr>
            </thead>
            <tbody>
              <tr class="filter">
                <td width="30%" class="center"><strong>ลำดับ</strong></td>
                <td width="70%" class="center"><strong>ชื่อสินค้า</strong></td>
              </tr>
<?php
	$num2=0;
	$sql2 = "SELECT name FROM product ORDER BY selling ASC LIMIT 10";
	$query2 = mysql_query($sql2) or die ("Error Query [".$sql2."]");
	while($row2=mysql_fetch_array($query2)){
	$num2++;
?>
              <tr>
                <td class="center"><?php echo $num2; ?></td>
                <td class="left" style="padding-left:30px"><?php echo $row2["name"]; ?></td>
              </tr>
<?php
}
?>
            </tbody>
          </table></td>
        </tr>
      </table>
      </div>
	  </div>
    </div>
  </div>
</div>
<?php mysql_close(); ?>
</body>
</html>
